﻿
function reload(){
	window.location.reload();
}
function help(msg){
	alert(msg);
}
function to(url){
	window.location.href=url;
}
function back(){
	history.go(-1);
}
function save(url,msg){
	alert(msg);
	to(url);
}
function add(url){
	alert('新建成功！');
	to(url);
}
function del(msg){
	if (window.confirm(msg)){
		reload();
	}
}

function checkEnter() {
	if(event.keyCode == 13)
		event.keyCode = 9;
}

function getCurTime(){
	var now=new Date();
	var year=now.getYear();
	var month=(now.getMonth())+1;
	var day=now.getDate();
	var hours=now.getHours();
	var minutes=now.getMinutes();
	var seconds=now.getSeconds();
	return year+"-"+month+"-"+day;
}

function setCurTime(oid){
	var oCtl = document.getElementById(oid);
	if(oCtl != null) {
		oCtl.value = getCurTime();
	}
}

function HTMLEncode ( input )
{
    var converter = document.createElement("DIV");
    converter.innerText = input;
    var output = converter.innerHTML;
    converter = null;
    return output;
}
function HTMLDecode ( input )
{
    var converter = document.createElement("DIV");
    converter.innerHTML = input;
    var output = converter.innerText;
    converter = null;
    return output;
}

function toNewLine(s) {
	return s.replace(/<br \/>/g, "\r\n");
}

function trim(s){
	return s.replace(/(^\s*)|(\s*$)/g, "");
}

function findParent(n, tag){
	if(n.parentNode.tagName == tag) {
		return n.parentNode;
	} else {
		return findParent(n.parentNode, tag);
	}
}

function showTitle(obj){
	$("tr.title").hide();
	$("tr#" + obj).show();
}

function changeColor(sender, $tr, checked) {
	if ($tr == null){
		$tr = $(findParent(sender, "TR"));
	}
	if (checked == null){
		checked = true;
	}
	var $input = $(sender);
	if(checked && $input.attr("checked")){
		$tr.children("td").addClass("selected");
	}else{
		$tr.children("td").removeClass("selected");
	}
}

function checkAllChecker() {
	var input = $(event.srcElement);
	if(input.attr("checked")){
		$("input[name=checker]").attr("checked", "true");
	}else{
		$("input[name=checker]").removeAttr("checked");
	}
	// 为统计页面的克隆表头提供选中同步
	if (input.attr("id") == "clone") {
		if(input.attr("checked")){
			$("#target").attr("checked", "true");
		}else{
			$("#target").removeAttr("checked");
		}
	}
}

/* 删除选中的单据 For 两个查询以及两个统计 */
var trs;
function deleteSelected(type, isClose) {
	var $input;
	var insertIndex = 0;
	var ids = "";
	trs = [];
	$("input[name=checker][type=checkbox]").each(function(index, element) {
		$input = $(element);
		if ($input.attr("checked")) {
			trs[insertIndex] = $input.parents("tr");
			ids = ids + "'" + trim($input.val()) + "',";
			insertIndex++;
		}
    });
	
	if(trs.length < 1) {
		return;
	}
	
	ids = ids.substr(0, ids.length - 1);
	
	if (isClose) {
		get_cele_date(event.srcElement, 
			function(closeDate) {
				sendItems(ids, type, isClose, trs, closeDate);
			}
		);
	} else {
		sendItems(ids, type, isClose, trs);
	}
}
function sendItems(ids, type, isClose, trs, closeDate) {
	$.post("deleteSelected.do", { ids: ids, type: type, isClose: isClose, closeDate: isClose ? closeDate : "" },
		function (data) {
			for(var i = 0; i < trs.length; i++){
				if(!isClose){
					trs[i].remove();
				}else{
					var tds = trs[i].children("td");
					changeColor(trs[i].find("input[name=checker][type=checkbox]") , trs[i], false);
					tds[0].innerHTML = "";
					if (tds[tds.length - 1].innerHTML.indexOf("修改") > -1) tds[tds.length - 1].innerHTML = "";
					trs[i].children("td.closeDate").text(closeDate);
				}
			}
		},
		"xml"
	);
}

/* 以下为页面加载完成后，自执行函数 */

// 页面加载完成后 克隆固定表头
function cloneHead() {
	if($("#fixedHead").length > 0) return; // 如果已经克隆，则取消克隆
	
	var tr = $("#mainTable tr:first");
	if (tr.length < 1) return; // 如果不存在数据表，则取消克隆
	
	var cloneTr = tr.clone(true);
	var input = tr.find("input[type=checkbox]");
	input.attr("id", "target");
	var cloneInput = cloneTr.find("input[type=checkbox]");
	cloneInput.attr("id", "clone");
	if(input.attr("checked")) {
		cloneInput.attr("checked", "true");
	}
		
	$("body").append(
		$("<div id='fixedHead' style='display: none;'></div>").append($("<table id='cloneTable' class='data_list_table'></table>").append(cloneTr))
	);
	
	$("#cloneTable").width($("#mainTable").width());
	$("#mainTable tr:eq(1) td").each(function(index, element) {
		var a = cloneTr.find("th:eq(" + index + ")");
		a.width($(element).width());
		
		//alert($(element).width() + " : " + a.width());
    });
}

// 如果页面存在文本域 则设置提示输入长度
function setAlertLength(){
	var text = $(".alertLengthTarget").text();
	var len = 500 - text.length;
	var $lengthSpan = $("#alertLength");
	$lengthSpan.text(len);
	if(len < 0){
		
		$lengthSpan.css("color","red");
	}else{
		$lengthSpan.css("color", $lengthSpan.attr("lastColor"));
	}
}
function initSetAlertLength(){
	var span = $("#alertLength");
	span.attr("lastColor", span.css("color"));
	if (span.length > 0) {
		var td = findParent(span[0], "TD");
		var txt = $(td).find("textarea");
		if(txt.length > 0) {
			txt.addClass("alertLengthTarget");
			txt.keyup(function(e) {
				setAlertLength();
			});
		}
	}
}

// 设置表格宽度
function setWidth(){
	var $div = $("#setWidth");
	if ($div.length > 0) {
		/*$div.children("div").width(
			$div.find("#mainTable").width() < $(document).width() ? 
			$(document).width() :
			$div.find("#mainTable").width()
		);*/
		$div.children().width($(document).width());
	}
}

// 设置表尾统计位置
function setFooterPadding() {
	var $td = $(".footer");
	if ($td.length > 0) {
		var colspan = $td.parents("tr").children("th:eq(0)").attr("colspan");
		var width = $td.parents("table").find("tr:eq(1)").children("td:eq(" + colspan + ")").width();
		$td.css("text-align", "left");
		$td.css("padding-left", width);
	}
}

// 设置千分位
function setSeparators() {
	var re=/(\d{1,3})(?=(\d{3})+(?:$|\.))/g;
	var $element;
	var s;
	$(".separators").each(function(index, element) {
		$element = $(element);
		s = $element.text();
		if(s.length - s.indexOf(".") == 5)
			s = s.substring(0, s.length - 2);
        $element.text(s.replace(re,"$1,"));
    });
}

// 统计价格
function sumPrice() {
	var total = 0;
	var $prices = $(".sumPrice");
	if ( $prices.length > 0) {
		$prices.each(function(index, element) {
			total += parseFloat($(element).text().replace(/,/, "")); //除去千分符 .replace(/,/, "")
		});
		$("#sumPrice").text(total);
	}
}

$(document).ready(function(e) {
	//setWidth();
	//cloneHead();
	initSetAlertLength();
	sumPrice();
	setSeparators();
	// 设置Button外观
	$("button, .button_bar input[type=button]").button();
	//setFooterPadding(); 暂时不适用
});